package com.wang.tool.executor.transitivethreadcontext;

/**
 * tl切换器
 *
 * @Author shaopeng
 * @Date 2021/11/14
 */
public class SimpleThreadLocalTransmitter implements ThreadContextTransmitter {

    /**
     * tl
     */
    private ThreadLocal threadLocal;

    public SimpleThreadLocalTransmitter() {
    }

    public SimpleThreadLocalTransmitter(ThreadLocal threadLocal) {
        this.threadLocal = threadLocal;
    }

    @Override
    public Object get() {
        return threadLocal != null ? threadLocal.get() : null;
    }

    @Override
    public void set(Object obj) {
        if (threadLocal == null) {
            return;
        }
        if (obj == null) {
            threadLocal.remove();
        } else {
            threadLocal.set(obj);
        }
    }

    public ThreadLocal getThreadLocal() {
        return threadLocal;
    }

    public void setThreadLocal(ThreadLocal threadLocal) {
        this.threadLocal = threadLocal;
    }
}
